package com.wzcl.app.controller;


import com.wzcl.app.dao.model.HomeMassage;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.common.ValidList;
import com.wzcl.app.model.dto.HomeMassageDto;
import com.wzcl.app.model.vo.HomeMassageVo;
import com.wzcl.app.service.HomeMassageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/home")
@Api(value = "首页信息管理controller", tags = {"首页信息管理接口"})
public class HomeMassageController {

    @Autowired
    private HomeMassageService homeMassageService;

    @ApiOperation("修改首页信息")
    @PostMapping("/update/{type}/{groupName}")
    public RVO<HomeMassageVo> updateHomeMassage(@PathVariable String type, @PathVariable String groupName, @RequestBody @Valid ValidList<HomeMassageDto> homeMassageDtoList) {
        List<HomeMassage> homeMassage = homeMassageService.updateHomeMassageList(type,groupName,homeMassageDtoList);
        if(homeMassage != null && homeMassage.size()>0){
            List<HomeMassageVo> collect = homeMassage.stream().map(HomeMassageVo::create).collect(Collectors.toList());
            RVO.success(collect);
        }
        return RVO.success(null);
    }

    @ApiOperation("获取首页信息")
    @PostMapping("/get")
    public RVO<HomeMassageVo> getHomeMassage(@RequestBody HomeMassageDto homeMassageDto) {
        HomeMassage homeMassage = homeMassageService.getHomeMassageById(homeMassageDto.getId());
        if(homeMassage != null){
            return RVO.success(HomeMassageVo.create(homeMassage));
        }
        return RVO.success(null);
    }

    @ApiOperation("查找首页信息")
    @PostMapping("/findList")
    public RVO<List<HomeMassageVo>> findList(@RequestBody HomeMassageDto homeMassageDto) {
        List<HomeMassage> homeMassageList = homeMassageService.getHomeMassageList(homeMassageDto);
        if(homeMassageList != null && homeMassageList.size() > 0){
            List<HomeMassageVo> collect = homeMassageList.stream().map(HomeMassageVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }

    @ApiOperation("查找首页信息")
    @PostMapping("/findPage")
    public RVO<List<HomeMassageVo>> findPage(@RequestBody HomeMassageDto homeMassageDto) {
        MyPage<HomeMassage> homeMassageList = homeMassageService.getHomeMassagePage(homeMassageDto);
        if(homeMassageList != null && homeMassageList.getContent() != null  &&homeMassageList.getContent().size()> 0){
            List<HomeMassageVo> collect = homeMassageList.getContent().stream().map(HomeMassageVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }
}
